﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace JetBlack.Text
{
    /// <summary>
    /// String extension methods.
    /// </summary>
    internal static class StringExtensions
    {
        /// <summary>
        /// Returns a string representation of a dictionary.
        /// </summary>
        /// <param name="dictionary">The dictionary to stringify.</param>
        /// <returns>A string representation of the dictionary.</returns>
        public static string ToDelimitedString(this IDictionary<string, object> dictionary)
        {
            return ToDelimitedString(dictionary, ",");
        }

        /// <summary>
        /// Returns a string representation of a dictionary.
        /// </summary>
        /// <param name="dictionary">The dictionary to stringify.</param>
        /// <param name="delimiter">The delimiter with which dictionary items are delimited.</param>
        /// <returns>A string representation of the dictionary.</returns>
        public static string ToDelimitedString(this IDictionary<string, object> dictionary, string delimiter)
        {
            return ToDelimitedString(dictionary, delimiter, ":");
        }

        /// <summary>
        /// Returns a string representation of a dictionary.
        /// </summary>
        /// <param name="dictionary">The dictionary to stringify.</param>
        /// <param name="itemDelimiter">The string with which items in the dictionary are delimited.</param>
        /// <param name="pairDelimiter">The string with which pairs in the dictionary are delimited.</param>
        /// <returns>A string representation of the dictionary.</returns>
        public static string ToDelimitedString(this IDictionary<string, object> dictionary, string itemDelimiter, string pairDelimiter)
        {
            return dictionary.ToDelimitedString(itemDelimiter, x => string.Concat(x.Key, pairDelimiter, x.Value));
        }

        /// <summary>
        /// Returns a string representation of an enumeration.
        /// </summary>
        /// <typeparam name="T">The type of the enumeration members.</typeparam>
        /// <param name="collection">The enumeration.</param>
        /// <param name="delimiter">The delimiter with which to seperate the items.</param>
        /// <param name="toString">A delegate which represents the items as a string.</param>
        /// <returns>A string representation of the collection.</returns>
        public static string ToDelimitedString<T>(this IEnumerable<T> collection, string delimiter, Func<T, string> toString)
        {
            return string.Join(delimiter, collection.Select(s => toString.Invoke(s)).ToArray());
        }
    }
}
